﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
namespace Tracy.CoreLib.Data
{
    public class ResourceItem : ManagedXmlModel_CommonFunctions, IManagedXmlModelItem
    {
        #region 构造函数
        public ResourceItem() : base() { }
        public ResourceItem(string xml) : base(xml) { }
        public ResourceItem(XmlNode node) : base(node) { }
        #endregion

        public override void SetValue(string key, string value)
        {
            switch (key)
            {
                case Constants.CONST_TAG_LASTUPDATE: LastUpdate = Convert.ToDateTime(value); break;
                default: base.SetValue(key, value); break;
            }
        }

        public string XmlNodeName
        {
            get { return Constants.CONST_TAG_ITEM; }
        }

        private DateTime lastUpdate;
        public DateTime LastUpdate
        {
            get { if (lastUpdate == null)lastUpdate = new DateTime(); return lastUpdate; }
            set
            {
                lastUpdate = value;
                Data[Constants.CONST_TAG_LASTUPDATE] = value.ToString();
            }
        }


        public string Title
        {
            get
            {
                if (Data.ContainsKey(Constants.CONST_TAG_TITLE)) return Data[Constants.CONST_TAG_TITLE];
                else return "";
            }
            set
            {
                Data[Constants.CONST_TAG_TITLE] = value;
            }
        }

        public string Description
        {
            get
            {
                if (Data.ContainsKey(Constants.CONST_TAG_DESCRIPTION)) return Data[Constants.CONST_TAG_DESCRIPTION];
                else return "";
            }
            set
            {
                Data[Constants.CONST_TAG_DESCRIPTION] = value;
            }
        }

        public string Link
        {
            get
            {
                if (Data.ContainsKey(Constants.CONST_TAG_LINK)) return Data[Constants.CONST_TAG_LINK];
                else return "";
            }
            set
            {
                Data[Constants.CONST_TAG_LINK] = value;
            }
        }

        public string Type
        {
            get
            {
                if (Data.ContainsKey(Constants.CONST_TAG_TYPE)) return Data[Constants.CONST_TAG_TYPE];
                else return "";
            }
            set
            {
                Data[Constants.CONST_TAG_TYPE] = value;
            }
        }

        public string FileName
        {
            get
            {
                if (Data.ContainsKey(Constants.CONST_TAG_FILENAME)) return Data[Constants.CONST_TAG_FILENAME];
                else return "";
            }
            set { Data[Constants.CONST_TAG_FILENAME] = value; }
        }

        public string Folder
        {
            get
            {
                if (Data.ContainsKey(Constants.CONST_TAG_FOLDER)) return Data[Constants.CONST_TAG_FOLDER];
                else return "";
            }
            set { Data[Constants.CONST_TAG_FOLDER] = value; }
        }

        public string Status
        {
            get
            {
                if (Data.ContainsKey(Constants.CONST_TAG_STATUS)) return Data[Constants.CONST_TAG_STATUS];
                else return "";
            }
            set { Data[Constants.CONST_TAG_STATUS] = value; }
        }

        public override string ToString()
        {
            return Title;
        }

        protected override void PreParseXml(XmlNode node)
        {
            Title = SelectNode(node, Constants.CONST_TAG_TITLE, true).InnerText;
            //description = SelectNode(node, Constants.CONST_TAG_DESCRIPTION, true).InnerText;
            Link = SelectNode(node, Constants.CONST_TAG_LINK, true).InnerText;
            Type = SelectNode(node, Constants.CONST_TAG_TYPE, true).InnerText;
            FileName = SelectNode(node, Constants.CONST_TAG_FILENAME, true).InnerText;
            Status = SelectNode(node, Constants.CONST_TAG_STATUS, true).InnerText;
            Folder = SelectNode(node, Constants.CONST_TAG_FOLDER, true).InnerText;
            LastUpdate = Convert.ToDateTime(SelectNode(node, Constants.CONST_TAG_LASTUPDATE, true).InnerText);
        }

        //protected override void GenerateCustomizedXml(XmlDocument doc, XmlNode newNode)
        //{
        //    AppendTextChildNode(doc, newNode, Constants.CONST_TAG_TITLE, Title);
        //    AppendTextChildNode(doc, newNode, Constants.CONST_TAG_DESCRIPTION, Description);
        //    AppendTextChildNode(doc, newNode, Constants.CONST_TAG_LINK, Link);
        //    AppendTextChildNode(doc, newNode, Constants.CONST_TAG_TYPE, Type);
        //    AppendTextChildNode(doc, newNode, Constants.CONST_TAG_FILENAME, FileName);
        //    AppendTextChildNode(doc, newNode, Constants.CONST_TAG_FOLDER, Folder);
        //    AppendTextChildNode(doc, newNode, Constants.CONST_TAG_STATUS, Status);
        //    AppendTextChildNode(doc, newNode, Constants.CONST_TAG_LASTUPDATE, LastUpdate.ToString());
        //}
        
        //public override bool Equals(object obj)
        //{
        //    if (obj is ResourceItem)
        //    {
        //        ResourceItem item = (ResourceItem)obj;
        //        //TODO:有待进一步优化
        //        return (this.Link == item.Link);
        //    }
        //    else
        //    {
        //        return base.Equals(obj);
        //    }
        //}

        #region IKeyedItem Members

        public string Key
        {
            get { return Link; }
        }

        #endregion
    }

    public class ResourceProvider : XmlModelProvider_CommonFunctions, IXmlModelProvider
    {
        #region 构造函数
        public ResourceProvider() : base() { }
        public ResourceProvider(string xml) : base(xml) { }
        public ResourceProvider(XmlNode node) : base(node) { }
        #endregion

        public IXmlModelItem GenerateChild(System.Xml.XmlNode node)
        {
            return new ResourceItem(node);
        }

        public string XmlNodeName
        {
            get { return Constants.CONST_TAG_RESOURCELIST; }
        }


        public void UpdateStatusByLink(string link, string status)
        {
            if (Items.Contains(link) && Items[link] != null)
            {
                ((ResourceItem)Items[link]).Status = status;
            }
        }

        public ResourceItem GetResourceByLink(string link)
        {
            return (ResourceItem)Items[link];
        }

        public void UpdateStatusById(int id, string status)
        {
            ResourceItem item = GetResourceById(id);
            if (item != null) item.Status = status;
        }

        public void UpdateStatus(ResourceItem item, string status)
        {
            if (item != null) item.Status = status;
        }

        public ResourceItem GetResourceById(int id)
        {
            return (ResourceItem)((IManagedXmlModelCollection)Items).GetItemById(id);
        }

    }
}
